//
//  KyNetworkMannager+extension.swift
//  KySwiftPro
//
//  Created by fangyukui on 2017/10/23.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit

extension KyNetworkManager{
    
    func getStatues(since_id:Int64,max_id:Int64,complete:@escaping (_ json:[[String:Any]]?,_ isSucess:Bool)->()){
        let url  = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params  = ["since_id":since_id,"max_id":max_id > 0 ? max_id - 1 : 0 ]
        tokenRequest(URLString: url, params: params) { (json, isSucess) in
            guard let result = (json as? [String:Any])?["statuses"] as? [[String:Any]] else{
                complete(nil,isSucess)
                return
            }
            complete(result,isSucess)
        }
    }

    func getUnread_count(complete:@escaping (_ count:Int)->()) {
        let url = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid":"5365823342"]
        
        tokenRequest(URLString: url, params: params) { (json, isSucess) in
            let count  =  (json as? [String:Any])?["status"] as? Int ?? 0
            
            complete(count)
            
        }
       
    }
    
//    client_id	true	string	申请应用时分配的AppKey。
//    client_secret	true	string	申请应用时分配的AppSecret。
//    grant_type
    
    
    
    func requestAccessTocken(AuthoCode:String,complete:@escaping (_ json:[[String:Any]]?,_ isSucess:Bool)->()) {
        let url = "https://api.weibo.com/oauth2/access_token"
        let params  = ["client_id":"\(App_Key)","client_secret":"\(App_Secret)","grant_type":"authorization_code","code":"\(AuthoCode)","redirect_uri":"\(RedirectUrl)"]
        request(method: .POST, URLString: url, params: params) { (json, isSucess) in
            complete(json as? [[String : Any]],isSucess)
            
            KyLog(message: json)
        }
        
        
        
    }
    
}
